// pages/cart/cart.js
import { showToast } from "../../showToast/index.js"
import { showModal } from "../../showModal/index.js"
Page({

    /**
     * 页面的初始数据
     */
    data: {
     address:{},
     cart:[],
     allchecked:false,
     totalPrice:0,
     totalNum:0
    },
   async hanlePay(){
    const {address,totalNum}=this.data;
    if(!address.userName){
        await showToast({title:'你还没有选择收货地址'})
        return
    }
    if(totalNum===0){
        await showToast({title:'你还没有选购商品'})
        return
    }
    wx.navigateTo({
      url: '/pages/pay/pay',
    })
    },
    async handleItemNumEdit(e){
        const {operation,id}=e.currentTarget.dataset;
        let {cart}=this.data;
        const index=cart.findIndex(v=>v.data.message.goods_id===id);
        if(cart[index].num===1&&operation===-1){      
        const res=await showModal({content: '您是否要删除？'});
        if (res.confirm) {
          cart.splice(index,1);
          this.setCart(cart);
        }
        }else{
          cart[index].num+=operation;
          this.setCart(cart);
        }  
      },
    allchange(){
        let {cart,allchecked}=this.data;
        allchecked=!allchecked
        cart.forEach(v=>v.checked=allchecked)
        this.setCart(cart)
    },
    chooseAdd(){
     wx.chooseAddress({
       success: (address) => {
           wx.setStorageSync('address', address)
       },
     })
    },
    changestatus(e){
       const goods_id=e.currentTarget.dataset.goods_id
       let {cart}=this.data;
       let index=cart.findIndex((v)=>v.data.message.goods_id===goods_id);
       cart[index].checked=! cart[index].checked
       this.setData(cart)
    },
    setCart(cart){
    let allchecked=true
    let totalPrice=0
    let  totalNum=0
         cart.forEach(v=>{
         if(v.checked){
             totalPrice +=v.num*v.data.message.goods_price;
             totalNum +=v.num
         }else{
             allchecked=false
         }
     })
     allchecked=cart.length !=0?allchecked:false
             this.setData({
              cart,
              allchecked,
              totalPrice,
              totalNum
          })
          wx.setStorageSync('cart', cart)
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
          const  address=wx.getStorageSync('address');
          const cart=wx.getStorageSync('cart') || []
          this.setData({address})
          this.setCart(cart)
        //   const allchecked=cart.length?cart.every((v)=>v.checked):false
    //       let allchecked=true
    //      let totalPrice=0
    //     let  totalNum=0
    //  cart.forEach(v=>{
    //      if(v.checked){
    //          totalPrice +=v.num*V.data.message.goods_price;
    //          totalNum +=v.num
    //      }else{
    //          allchecked:false
    //      }
    //  })
    //  allchecked=cart.length !=0?allchecked:false
    //       this.setData({
    //           address,
    //           cart,
    //           allchecked,
    //           totalPrice,
    //           totalNum
    //       })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})